﻿#include "stdafx.h"
#include "AstLet.h"
#include "SiContext.h"
#include "AstCall.h"
#include "AutoType.h"
#include "CodeGenerate/LetGen.h"
#include <llvm/IR/LLVMContext.h>

CodeGen * AstLet::makeGen(SiContext * parent)
{
	auto *v = parent->findSymbolValue(name);
	if (!v) throw std::runtime_error("找不到变量:" + name);

	auto p = right->makeGen(parent);
	if (!p)
		throw std::runtime_error("不能为 "+name+"赋值");
	auto *x=dynamic_cast<AutoType*>(v->type);

	if (x) {	// 左值需要推断	
		x->setDeductive(p->type);
		std::cout << "推断：" << name<< " 为 " << p->type->name << std::endl;
	}

	auto o = new LetGen();
	o->left = parent->findSymbolValue(name, true);
	o->right = p;
	return o;
}

void AstLet::draw(std::ostream & os)
{
	dotLable(os, name + "=");
	dotPointTo(os, right);
}
